﻿namespace SmartSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using NLog;
    
    public class DmTsDataAccess
    {
        private static Logger log = LogManager.GetCurrentClassLogger();
        private void DeleteDepreciation(SqlConnection po_Connection, SqlTransaction po_Transaction, string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ts1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ts2", pd_Ngay_Ct2) };
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs4_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
        }

        public bool DeleteDetailRow(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt0", po_KeyCode), new SqlParameter() };
            values[1].Direction = ParameterDirection.ReturnValue;
            values[1].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs0_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                flag = ((int) values[1].Value) == 1;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return flag;
        }

        public bool DeleteDetailRow0(object po_MasterKey, object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt0", po_KeyCode), new SqlParameter() };
            values[1].Direction = ParameterDirection.ReturnValue;
            values[1].SqlDbType = SqlDbType.Int;
            SqlParameter[] parameterArray2 = new SqlParameter[] { new SqlParameter("p_Stt", po_MasterKey) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs0_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                command.CommandText = "DmTs1_Save";
                command.Parameters.Clear();
                command.Parameters.AddRange(parameterArray2);
                command.ExecuteNonQuery();                
                flag = ((int) values[1].Value) == 1;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return flag;
        }

        public bool DeleteDetailRow2(object po_MasterKey, object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt2", po_KeyCode), new SqlParameter() };
            values[1].Direction = ParameterDirection.ReturnValue;
            values[1].SqlDbType = SqlDbType.Int;
            SqlParameter[] parameterArray2 = new SqlParameter[] { new SqlParameter("p_Stt", po_MasterKey) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs2_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                command.CommandText = "DmTs1_Save";
                command.Parameters.Clear();
                command.Parameters.AddRange(parameterArray2);
                command.ExecuteNonQuery();                
                flag = ((int) values[1].Value) == 1;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception);
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return flag;
        }

        public bool DeleteDetailRow3(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt3", po_KeyCode), new SqlParameter() };
            values[1].Direction = ParameterDirection.ReturnValue;
            values[1].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs3_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                flag = ((int) values[1].Value) == 1;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception);
                throw new NotSupportedException("Invalid expression: ", exception);
            }            
            return flag;
        }

        public DataTable Depreciation(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ts1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ts2", pd_Ngay_Ct2) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "Tinh_Khau_Hao";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);                
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return table2;
        }

        public DataSet GetDataSet(string ps_Subsidiary)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");                
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public DataSet GetDataSet(string ps_Subsidiary, string ps_Loai_Ts)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Loai_Ts", ps_Loai_Ts)};
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");                
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public DataSet GetDataSetByParent(string ps_Subsidiary, string ps_Loai_Ts, string ps_ParentCode)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Loai_Ts", ps_Loai_Ts), new SqlParameter("p_Ma_Nh_Ts", ps_ParentCode) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");                
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public DataTable GetDepreciation(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ts1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ts2", pd_Ngay_Ct2) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs4_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);                
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return table2;
        }

        public DataTable GetDetailTable(object ps_KeyCode)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", ps_KeyCode) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs0_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);                
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return table2;
        }

        public DataTable GetDetailTable0(object ps_KeyCode)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", ps_KeyCode) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs0_Edit_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);                
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return table2;
        }

        public DataTable GetDetailTable2(object ps_KeyCode)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", ps_KeyCode) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs2Bp_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);                
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return table2;
        }

        public DataTable GetDetailTable3(object ps_KeyCode)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", ps_KeyCode) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs3_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);                
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return table2;
        }

        public DataRow GetMasterDataRow(object ps_KeyCode)
        {
            DataRow row;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", ps_KeyCode) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs_One_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);                
                if (dataTable.Rows.Count == 0)
                {
                    dataTable.Rows.Add(dataTable.NewRow());
                    dataTable.AcceptChanges();
                }
                row = dataTable.Rows[0];
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return row;
        }

        public bool Save(bool pb_NewChecked, DataRow po_MasterDataRow, DataTable po_DetailDataTable)
        {
            bool flag = false;
            foreach (DataRow row in po_DetailDataTable.Rows)
            {
                decimal tien0 = (decimal)row["Tien0"];
                if (tien0 == 0)
                {
                    log.Fatal(po_MasterDataRow["Ma_Ts"].ToString());
                }

                if ((row.RowState != DataRowState.Deleted) && (((decimal) row["Tien0"]) != 0M))
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                log.Info(string.Format("Delete MasterRow: {0} {1}", po_MasterDataRow["Ma_Ts"].ToString(), po_MasterDataRow["Stt"].ToString()));
                SSDataAccess.Delete("DMTS", po_MasterDataRow["Stt"]);
                return false;
            }
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlTransaction transaction = SSConnection.BeginTransaction();
            flag = true;
            foreach (DataRow row2 in po_DetailDataTable.Rows)
            {
                row2.EndEdit();
                if ((row2.RowState != DataRowState.Deleted) && (((decimal) row2["Tien0"]) != 0M))
                {
                    if (((row2.RowState != DataRowState.Added) && (row2.RowState != DataRowState.Modified)) || (this.SaveDetailRow(SSConnection, transaction, po_MasterDataRow["Stt"], row2) == 1))
                    {
                        continue;
                    }
                    flag = false;
                    break;
                }
                if (row2.RowState == DataRowState.Deleted)
                {
                    this.DeleteDetailRow(SSConnection, transaction, row2["Stt0", DataRowVersion.Original]);
                }
                else
                {
                    this.DeleteDetailRow(SSConnection, transaction, row2["Stt0"]);
                }
            }
            if (flag && (this.SaveMasterRow(SSConnection, transaction, pb_NewChecked, po_MasterDataRow) == 1))
            {                
                po_DetailDataTable.AcceptChanges();
                po_MasterDataRow.Table.AcceptChanges();
                transaction.Commit();
                SqlCommand command = new SqlCommand();
                SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", po_MasterDataRow["Stt"]) };
                try
                {
                    command.CommandText = "DmTs1_Save";
                    command.CommandType = CommandType.StoredProcedure;
                    command.Connection = SSConnection;
                    command.Parameters.AddRange(values);
                    command.ExecuteNonQuery();
                }
                catch (Exception exception)
                {
                    log.ErrorException(po_MasterDataRow["Ma_Ts"].ToString(), exception); 
                    throw new NotSupportedException("Invalid expression: ", exception);
                }
                SSDataAccess.CloseConnection(SSConnection);
                return true;
            }
            transaction.Rollback();
            SSDataAccess.CloseConnection(SSConnection);
            return false;
        }

        public bool Save3(object ps_KeyCode, DataTable po_DetailDataTable)
        {
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlTransaction transaction = SSConnection.BeginTransaction();
            bool flag = true;
            foreach (DataRow row in po_DetailDataTable.Rows)
            {
                row.EndEdit();
                if ((row.RowState != DataRowState.Deleted) && ((((decimal) row["So_Luong"]) != 0M) || (((decimal) row["Tien"]) != 0M)))
                {
                    if (((row.RowState != DataRowState.Added) && (row.RowState != DataRowState.Modified)) || (this.SaveDetailRow3(SSConnection, transaction, ps_KeyCode, row) == 1))
                    {
                        continue;
                    }
                    flag = false;
                    break;
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    this.DeleteDetailRow3(SSConnection, transaction, row["Stt3", DataRowVersion.Original]);
                }
                else
                {
                    this.DeleteDetailRow3(SSConnection, transaction, row["Stt3"]);
                }
            }
            if (flag)
            {
                transaction.Commit();
                SSDataAccess.CloseConnection(SSConnection);
                return true;
            }
            transaction.Rollback();
            SSDataAccess.CloseConnection(SSConnection);
            return false;
        }

        public bool Save4(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2, DataTable po_DetailDataTable)
        {
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlTransaction transaction = SSConnection.BeginTransaction();
            this.DeleteDepreciation(SSConnection, transaction, ps_Subsidiary, pd_Ngay_Ct1, pd_Ngay_Ct2);
            foreach (DataRow row in po_DetailDataTable.Rows)
            {
                row.EndEdit();
                if ((row.RowState != DataRowState.Deleted) && (((((decimal) row["Tien2_N1"]) != 0M) || (((decimal) row["Tien2_N2"]) != 0M)) || (((((decimal) row["Tien2_N3"]) != 0M) || (((decimal) row["Tien2_N4"]) != 0M)) || (row["Edited"].ToString() == "C"))))
                {
                    this.SaveDepreciation(SSConnection, transaction, ps_Subsidiary, row);
                }
            }
            transaction.Commit();
            SSDataAccess.CloseConnection(SSConnection);
            return true;
        }

        private void SaveDepreciation(SqlConnection po_Connection, SqlTransaction po_Transaction, string ps_Subsidiary, DataRow po_Row)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Stt", po_Row["Stt"]), new SqlParameter("p_Tk_No", po_Row["Tk_No"]), new SqlParameter("p_Ma_Bp", po_Row["Ma_Bp"]), new SqlParameter("p_Ngay_KH", po_Row["Ngay_KH"]), new SqlParameter("p_Tien2_N1", po_Row["Tien2_N1"]), new SqlParameter("p_Tien2_N2", po_Row["Tien2_N2"]), new SqlParameter("p_Tien2_N3", po_Row["Tien2_N3"]), new SqlParameter("p_Tien2_N4", po_Row["Tien2_N4"]), new SqlParameter("p_Edited", po_Row["Edited"]) };
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs4_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression", exception);
            }
        }

        private int SaveDetailRow(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_MasterKey, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt0", row["Stt0"]), new SqlParameter("p_Stt", po_MasterKey), new SqlParameter("p_Ma_TGTS", row["Ma_TGTS"]), new SqlParameter("p_Ma_Nvon", row["Ma_Nvon"]), new SqlParameter("p_Tien0", row["Tien0"]), new SqlParameter("p_Tien1", row["Tien1"]), new SqlParameter("p_Tien3", row["Tien3"]), new SqlParameter("p_Tien4", row["Tien4"]), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[8].Direction = ParameterDirection.Output;
            values[8].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs0_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[8].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        public int SaveDetailRow0(object po_MasterKey, DataRow row)
        {
            int num;
            if (row == null)
            {
                return 0;
            }
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt0", row["Stt0"]), new SqlParameter("p_Stt", po_MasterKey), new SqlParameter("p_Ngay_Ps", row["Ngay_Ps"]), new SqlParameter("p_So_Ct_Ps", row["So_Ct_Ps"]), new SqlParameter("p_Ma_TGTS", row["Ma_TGTS"]), new SqlParameter("p_Ma_Nvon", row["Ma_Nvon"]), new SqlParameter("p_Dien_Giai", row["Dien_Giai"]), new SqlParameter("p_Desc_EN", row["Desc_EN"]), new SqlParameter("p_Tien0", row["Tien0"]), new SqlParameter("p_So_Thang", row["So_Thang"]), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[10].Direction = ParameterDirection.Output;
            values[10].SqlDbType = SqlDbType.Int;
            SqlParameter[] parameterArray2 = new SqlParameter[] { new SqlParameter("p_Stt", po_MasterKey) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs0_Edit_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                command.CommandText = "DmTs1_Save";
                command.Parameters.Clear();
                command.Parameters.AddRange(parameterArray2);
                command.ExecuteNonQuery();                
                num = (int) values[10].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }

        public int SaveDetailRow2(object po_MasterKey, DataRow row)
        {
            int num;
            if (row == null)
            {
                return 0;
            }
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt2", row["Stt2"]), new SqlParameter("p_Stt", po_MasterKey), new SqlParameter("p_Ngay_Ps", row["Ngay_Ps"]), new SqlParameter("p_Ma_Bp", row["Ma_Bp"]), new SqlParameter("p_Tk_No", row["Tk_No"]), new SqlParameter("p_So_Thang", row["So_Thang"]), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[6].Direction = ParameterDirection.Output;
            values[6].SqlDbType = SqlDbType.Int;
            SqlParameter[] parameterArray2 = new SqlParameter[] { new SqlParameter("p_Stt", po_MasterKey) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs2BP_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                command.CommandText = "DmTs1_Save";
                command.Parameters.Clear();
                command.Parameters.AddRange(parameterArray2);
                command.ExecuteNonQuery();                
                num = (int) values[6].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }

        private int SaveDetailRow3(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_MasterKey, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt3", row["Stt3"]), new SqlParameter("p_Stt", po_MasterKey), new SqlParameter("p_Ma_Vt", row["Ma_Vt"]), new SqlParameter("p_Dien_Giai", row["Dien_Giai"]), new SqlParameter("p_Desc_EN", row["Desc_EN"]), new SqlParameter("p_Dvt", row["Dvt"]), new SqlParameter("p_So_Luong", row["So_Luong"]), new SqlParameter("p_Gia", row["Gia"]), new SqlParameter("p_Tien", row["Tien"]), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[9].Direction = ParameterDirection.Output;
            values[9].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs3_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[9].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        public int SaveMasterRow(SqlConnection po_Connection, SqlTransaction po_Transaction, bool pb_NewChecked, DataRow row)
        {            
            int num;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_Ma_Dvcs", row["Ma_Dvcs"]), new SqlParameter("p_Ma_Ts", row["Ma_Ts"]), new SqlParameter("p_The_Ts", row["The_Ts"]), new SqlParameter("p_So_Ct_T", row["So_Ct_T"]), new SqlParameter("p_Ten_Ts", row["Ten_Ts"]), new SqlParameter("p_Ten_Ts_E", row["Ten_Ts_E"]), new SqlParameter("p_So_Hieu", row["So_Hieu"]), new SqlParameter("p_Dien_Giai", row["Dien_Giai"]), new SqlParameter("p_Desc_EN", row["Desc_EN"]), new SqlParameter("p_Dvt", row["Dvt"]), new SqlParameter("p_So_Luong", row["So_Luong"]), new SqlParameter("p_Ma_Nh_Ts", row["Ma_Nh_Ts"]), new SqlParameter("p_Stt_Nh_Ts", row["Stt_Nh_Ts"]), new SqlParameter("p_Ma_Qg", row["Ma_Qg"]), 
                new SqlParameter("p_Nam_Sx", row["Nam_Sx"]), new SqlParameter("p_Ma_Bp", row["Ma_Bp"]), new SqlParameter("p_Ngay_Ts", row["Ngay_Ts"]), new SqlParameter("p_Ngay_KH", row["Ngay_KH"]), new SqlParameter("p_Cong_Suat", row["Cong_Suat"]), new SqlParameter("p_Tk", row["Tk"]), new SqlParameter("p_Ma_MDSD", row["Ma_MDSD"]), new SqlParameter("p_Tinh_KH", row["Tinh_KH"]), new SqlParameter("p_So_Thang", row["So_Thang"]), new SqlParameter("p_Tk_No", row["Tk_No"]), new SqlParameter("p_Tk_Co", row["Tk_Co"]), new SqlParameter("p_Ma_Sp", row["Ma_Sp"]), new SqlParameter("p_Ma_Km", row["Ma_Km"]), new SqlParameter("p_Loai_Ts", row["Loai_Ts"]), new SqlParameter("p_TTien0", row["TTien0"]), new SqlParameter("p_TTien1", row["TTien1"]), 
                new SqlParameter("p_TTien2", row["TTien2"]), new SqlParameter("p_TTien3", row["TTien3"]), new SqlParameter("p_TTien4", row["TTien4"]), new SqlParameter("p_Ma_Loai1", row["Ma_Loai1"]), new SqlParameter("p_Ma_Loai2", row["Ma_Loai2"]), new SqlParameter("p_Ma_Loai3", row["Ma_Loai3"]), new SqlParameter("p_Ma_NCC", row["Ma_NCC"]), new SqlParameter("p_Ma_NSX", row["Ma_NSX"]), new SqlParameter("p_Han_BH", row["Han_BH"]), new SqlParameter("p_Ngay_BDKH", row["Ngay_BDKH"]), new SqlParameter("p_Ma_Dvt", row["Ma_Dvt"]), new SqlParameter("p_Result", SqlDbType.BigInt)
             };
            values[0x26+5].Direction = ParameterDirection.Output;
            values[0x26+5].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTs_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[0x26+5].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(row["Ma_Ts"].ToString(), exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        public int TaiSanG_Delete(object pi_MasterKey)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", pi_MasterKey), new SqlParameter() };
            values[1].Direction = ParameterDirection.ReturnValue;
            values[1].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTsG_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();                
                num = (int) values[1].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }

        public void TaiSanG_Get(object pi_MasterKey, ref object pd_Ngay_Giam, ref object ps_So_Ct_G, ref object ps_Ma_Giam_Ts, ref object ps_Ten_Giam_Ts)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", pi_MasterKey), new SqlParameter("p_Ngay_Giam", pd_Ngay_Giam), new SqlParameter("p_So_Ct_G", ps_So_Ct_G), new SqlParameter("p_Ma_Giam_Ts", ps_Ma_Giam_Ts), new SqlParameter("p_Ten_TGTS", ps_Ten_Giam_Ts) };
            values[1].Direction = ParameterDirection.Output;
            values[1].SqlDbType = SqlDbType.DateTime;
            values[2].Direction = ParameterDirection.Output;
            values[2].SqlDbType = SqlDbType.NVarChar;
            values[2].Size = 20;
            values[3].Direction = ParameterDirection.Output;
            values[3].SqlDbType = SqlDbType.NVarChar;
            values[3].Size = 0x20;
            values[4].Direction = ParameterDirection.Output;
            values[4].SqlDbType = SqlDbType.NVarChar;
            values[4].Size = 0x80;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTsG_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();                
                pd_Ngay_Giam = values[1].Value;
                ps_So_Ct_G = values[2].Value;
                ps_Ma_Giam_Ts = values[3].Value;
                ps_Ten_Giam_Ts = values[4].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
        }

        public int TaiSanG_Save(object pi_MasterKey, object pd_Ngay_Giam, object ps_So_Ct_G, object ps_Ma_Giam_Ts)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", pi_MasterKey), new SqlParameter("p_Ngay_Giam", pd_Ngay_Giam), new SqlParameter("p_So_Ct_G", ps_So_Ct_G), new SqlParameter("p_Ma_Giam_Ts", ps_Ma_Giam_Ts), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[4].Direction = ParameterDirection.Output;
            values[4].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTsG_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();                
                num = (int) values[4].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }
    }
}

